package com.xd.core.business.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import com.xd.common.constant.SysConstant;
import com.xd.common.exception.XkdException;
import com.xd.common.util.OrderNumUtil;
import com.xd.common.util.PdfUtil;
import com.xd.common.util.SecurityUtil;
import com.xd.core.business.dto.IncomingInfoDto;
import com.xd.core.business.dto.IncomingQuery;
import com.xd.core.business.entity.Contract;
import com.xd.core.business.entity.IncomingInfo;
import com.xd.core.business.mapper.ContractMapper;
import com.xd.core.business.mapper.IncomingInfoMapper;
import com.xd.core.business.service.IContractService;
import com.xd.core.business.service.impl.IncomingInfoServiceImpl;
import com.xd.core.business.vo.*;
import com.xd.core.business.service.IIncomingInfoService;
import com.xd.core.debt.dto.Review;
import com.xd.core.mission.entity.MissionCenterInfo;
import com.xd.core.mission.mapper.MissionCenterInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static java.awt.SystemColor.info;

/**
 * <p>
 * 进件信息表 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2025-02-23
 */
@Slf4j
@Api(tags = "进件管理")
@RestController
@RequestMapping("/business/incoming-info")
public class IncomingInfoController {

    @Autowired
    private IIncomingInfoService iIncomingInfoService;
    @Autowired
    private MissionCenterInfoMapper missionCenterInfoMapper;
    @Autowired
    private IncomingInfoServiceImpl infoService;


    @PreAuthorize("hasAnyAuthority('sys:AddNewOrder')")
    @ApiOperation("新增进件")
    @PostMapping("/addIncoming")
    public void addIncoming(@RequestBody IncomingInfoDto info) {
        iIncomingInfoService.addIncoming(info);
    }

    @PreAuthorize("hasAnyAuthority('sys:NewPolicySubmission')")
    @ApiOperation("新增保单进件")
    @PostMapping("/addSlipIncoming")
    public void addSlipIncoming(@RequestBody IncomingInfoDto info) {
       iIncomingInfoService.addSlipIncoming(info);
    }

    @ApiOperation("授权二维码")
    @GetMapping("/qrTranslator/{incomingId}")
    public String qrTranslator(@PathVariable Integer incomingId) {
        return infoService.getIncomingUrl(incomingId,SysConstant.INCOMING_URL_TYPE_ONE,null);
    }

    @ApiOperation("签约二维码")
    @GetMapping("/ConTranslator/{incomingId}/{payPass}")
    public String ConTranslator(@PathVariable Integer incomingId, @PathVariable String payPass) {
        return infoService.getIncomingUrl(incomingId,SysConstant.INCOMING_URL_TYPE_TWO,payPass);
    }

    @ApiOperation("编辑保单进件")
    @PostMapping("/updateSlipIncoming")
    public void updateSlipIncoming(@RequestBody IncomingInfoDto info) {
        iIncomingInfoService.updateSlipIncoming(info);
    }

    @ApiOperation("保单进件返显详情")
    @GetMapping("/getSlipIncomingDetail/{incomingId}")
    public IncomingInfo addSlipIncoming(@PathVariable Integer incomingId) {
        return iIncomingInfoService.getById(incomingId);
    }


    @ApiOperation("保单--业务详情--进件信息")
    @GetMapping("/getSlipBusinessDetail/{incomingId}")
    public InsuranceIncomingCreditInfo getSlipBusinessDetail(@PathVariable Integer incomingId) {
        return iIncomingInfoService.getSlipBusinessDetail(incomingId);
    }

    @ApiOperation("保单--业务详情--进件信息--保司信息")
    @GetMapping("/getSlipBusinessInsuranceDetail/{incomingId}")
    public CreditInsuranceVo getSlipBusinessInsuranceDetail(@PathVariable Integer incomingId) {
        return iIncomingInfoService.getSlipBusinessInsuranceDetail(incomingId);
    }

    @PreAuthorize("hasAnyAuthority('sys:PolicyInstallment-viewDetails')")
    @ApiOperation("保单进件列表")
    @PostMapping("/insuranceIncomingList")
    public Page<InsuranceIncomingInfoPageVo> insuranceIncomingList(@RequestBody IncomingQuery query) {
        return iIncomingInfoService.insuranceIncomingList(query);
    }

    @ApiOperation("进件详情")
    @GetMapping("/getDetail/{id}")
    public IncomingInfoVo getDetail(@PathVariable Integer id) {
        return iIncomingInfoService.getDetail(id);
    }


    @PreAuthorize("hasAnyAuthority('sys:Incoming-copy','sys:Lending-copy')")
    @ApiOperation("重新进件详情")
    @GetMapping("/getReIncoming/{id}")
    public IncomingInfoVo getReIncoming(@PathVariable Integer id) throws IOException {
        return iIncomingInfoService.getReIncoming(id);
    }


    @PreAuthorize("hasAnyAuthority('sys:Incoming-viewDetails','sys:Incoming')")
    @ApiOperation("进件列表")
    @PostMapping("/IncomingList")
    public Page<IncomingInfoPageVo> getIncomingList(@RequestBody IncomingQuery query) {
        return iIncomingInfoService.incomingList(query);
    }


    @PreAuthorize("hasAnyAuthority('sys:Incoming-Export')")
    @ApiOperation("进件列表--导出excel")
    @PostMapping("/exportIncomingList")
    public void exportCustomerInfoList(@RequestBody IncomingQuery query) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        iIncomingInfoService.exportIncomingInfoList(query, missionCenterInfo.getId());
    }


    @PreAuthorize("hasAnyAuthority('sys:EditInformation')")
    @ApiOperation("编辑进件")
    @PostMapping("/updateIncoming")
    public void updateIncoming(@RequestBody IncomingInfoDto infoDto) {
        iIncomingInfoService.updateIncoming(infoDto);
    }

    @ApiOperation("平台审核")
    @PostMapping("/platformReview")
    public void updateStatus(@RequestBody Review review) {
        iIncomingInfoService.platformReview(review);
    }



}
